<?php
namespace Common\Model;
use Think\Model;

class AdminModel extends  Model {
    protected $_validate = array(
		array('name','require','管理员名称不得为空！',1),  // 都有时间都验证
		array('name','','管理员名称不得重复！',1,unique), 
		
		array('password','require','管理员密码不得为空！',1),
		);



public function getpri($roleid){
		$role=D('Role');
		$pri=D('Privilege');
		$role->field('rolename,prilist')->find($roleid);
		session('rolename',$role->rolename);
		if($role->prilist=='*'){
			session('privilege','*');
			$menu=$pri->where("pid=0")->select();
			foreach ($menu as $k => $v) {
				$menu[$k]['sub']=$pri->where('pid='.$v['id'])->select();
			}

			session('menu',$menu);

		}else{
			//Admin/Admin/add,Admin/Article/add
			
			$pris=$pri->field('id,pid,name,m,c,f,CONCAT(m,"/",c,"/",f) url')->where("id IN({$role->prilist})")->select();
			$_pris=array();
			$menu=array();
			foreach($pris as $k=>$v){
				$_pris[]=$v['url'];
				if($v['pid']==0){
				$menu[]=$v;
				}
			}
			session('privilege',$_pris);
			foreach ($menu as $k => $v) {
				foreach ($pris as $k1 => $v1) {
					if($v1['pid']==$v['id']){
						$menu[$k]['sub'][]=$v1;
					}
					
				}
			}

			session('menu',$menu);
		}

	}


    public function login($username,$password){
		$info=$this->where("name='".$username."'")->find();
		if($info){
			$id=$info['id'];
			if($info['password']==md5($password)){
				session('id',$info['id']);
				session('username',$info['name']);
				$this->getpri($info['roleid']);
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}


}
